<?php

declare(strict_types=1);

namespace App\Listener;

use App\Event\DataStaticEvent;
use App\Model\DataStatic;
use Hyperf\Event\Annotation\Listener;
use Psr\Container\ContainerInterface;
use Hyperf\Event\Contract\ListenerInterface;

/**
 * @Listener
 */
class DataStaticListener implements ListenerInterface
{
    /**
     * @var ContainerInterface
     */
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function listen(): array
    {
        return [
            DataStaticEvent::class
        ];
    }

    public function process(object $event)
    {
        $model = $event->model;
        $params = $event->params;
        $static = $model->static;
        if(!$static){
            $model->static()->save(new DataStatic([
                'pv' =>1,
                'uv' => 0
            ]));
        }else{
            $static->pv+=1 ;
            $static->save();
        }
    }
}
